# vim:ts=4:sw=4:noet:
""" Jump To dialog """

import wx

class JumpToDialog(wx.Dialog):

	def __init__(self, parent):
		wx.Dialog.__init__(self, parent, -1, "Jump To ...",
									 style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER)
		self.mw = parent
		self.SetMinSize((300,200))
		self.textctrl = wx.TextCtrl(self, -1, "", style = wx.TE_PROCESS_ENTER)
		self.textctrl.Bind(wx.EVT_TEXT, self.onUpdate)
		self.textctrl.Bind(wx.EVT_TEXT_ENTER, self.onEnter)
		self.textctrl.SetFocus()
		
		ok = wx.Button(self, wx.ID_OK, "OK")
		ok.SetDefault()
		cancel = wx.Button(self, wx.ID_CANCEL, "Cancel")
		
		dlgsizer = wx.BoxSizer(wx.VERTICAL)
		dlgsizer.Add(self.textctrl, 0, wx.EXPAND | wx.LEFT | wx.RIGHT | wx.TOP, 10)
		
		matchListId = wx.NewId()
		self.matchList = wx.ListBox(self, matchListId, choices=[" "," "])
		self.matchList.Bind(wx.EVT_LISTBOX_DCLICK, self.onListBoxSelect, id=matchListId)
		self.matchList.MoveAfterInTabOrder(self.textctrl)
		dlgsizer.Add(self.matchList, 1, wx.EXPAND | wx.LEFT | wx.RIGHT | wx.TOP, 10)
		
		btnsizer = wx.StdDialogButtonSizer()
		btnsizer.AddButton(ok)
		btnsizer.AddButton(cancel)
		btnsizer.Realize()
		
		dlgsizer.Add(btnsizer, 0, wx.ALL | wx.ALIGN_RIGHT, 10)
		
		self.SetSizer(dlgsizer)

		self.Fit()
		self.Centre()
		self.matchList.Clear()

	def onListBoxSelect(self, event):
		self.EndModal(wx.ID_OK)

	def onListBox(self, event):
		if self.matchList.GetCount():
			sel = self.matchList.GetSelection()
			if sel == wx.NOT_FOUND:
				self.matchList.SetSelection(0)

	def GetValue(self):
		sel = self.matchList.GetSelection()
		if sel == wx.NOT_FOUND:
			return self.textctrl.GetValue()
		else:
			return self.matchList.GetStringSelection()

	def onUpdate(self, evt):
		self.matchList.Clear()
		text = self.textctrl.GetValue().lower()
		if not text:
			return
		keys = self.mw.project.controller.getJumpkeys()
		for k in keys:
			if text in k:
				self.matchList.Append(k)

	def onEnter(self, evt):
		self.EndModal(wx.ID_OK)
